<?php
/*************************************************

用户公共控制器 - the 
Author: WangHuabin <17-10-20>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {

    public function index(){
    }

    //图形验证码
    public function img_verify(){
        $v_config = verify();
        $Verify =     new \Think\Verify($v_config);
        $Verify->codeSet = '0123456789';
        $Verify->entry();
    }
    //对比验证码
    function check_verify($code, $id = ''){    
        $verify = new \Think\Verify();  
        return $verify->check($code, $id);
    }

    //验证手机号码
    public function checkphone(){
        $phone = I('post.phone');
        $map['user_phone'] = $phone;
        $r = D('User')->get_one($map);
        if($r){
            $data = array('err'=>1,'msg'=>"手机号码已经注册！");
        }else{
            $data = array('err'=>0,'msg'=>"手机号码未注册！");
        }
        $this->ajaxReturn($data);
    }


    //注册
    public function register_first(){
        if(IS_POST){
            $phone = I('post.phone');
            $code = I('post.code');
            $map['phone'] = $phone;
            $map['status'] = 0;
            $map['role'] = 1;
            $codeinfo = D('Verification')->get_one($map);
            if($codeinfo){
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){
                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                    }else{
                        //验证码有效
                        $_SESSION['reg_phone'] = $phone;
                        $_SESSION['code_id'] = $codeinfo['id'];
                        //跳转到密码设置页面
                        $data = array('err'=>0);
                    }
                }else{
                    $data = array('err'=>3,'msg'=>'验证码错误！');
                }
            }else{
                    $data = array('err'=>2,'msg'=>'验证码无效！');
            }
            $this->ajaxReturn($data);
        }else{
            $this->display();
        }
    }

    //注册密码设置
    public function register_pass(){
       // echo  $_SESSION['code_id'];die;
        if(IS_POST){
            $phone = $_SESSION['reg_phone'];
            $password = I('post.password');
            $sid = I('post.sid');
            if($sid){
                 $postdata['referral'] = $sid;
                 //添加投顾记录
                 D('Recommend')->startTrans();
                 $recommen['add_time'] = time();
                 $recommen['user_id'] = $sid;
                 D('Recommend')->add($recommen);
                 //身份改为投顾
                 $t_data['role'] = 2;
                 $where['id'] = $sid;
                  D("User")->edit($where,$t_data);
            }

            $postdata['user_phone'] = $phone;
            $postdata['user_pass'] = I('post.password');
            $postdata['user_mixs'] = rand(111111,999999);
            $postdata['user_pass'] = md5($password.$postdata['user_mixs']);
            $postdata['user_name'] =rand(111,999).time();
            $postdata['nickname'] ="8号街".rand(1111,9999);
            $postdata['url_img'] =C('DOMAIN')."/App/Home/Public/images/default_photo.jpg";

             D('User')->startTrans();
            $res = D("User")->add($postdata);
            if($res){
                //用户信息拓展表
                $ip = get_client_ip_user();
                            $info_m = array(
                                'id_user'      => $res,
                                'form'         => 1,
                                'invite_code'  => '',
                                'joint_type'   => 0,
                                'joint_id'     => '',
                                'joint_token'  => '',
                                'joint_val'    => '',
                                'randpass'     => 0,
                                'verify_phone' => 1,
                                'verify_email' => 0,
                                'time_last'    => time(),
                                'time_add'     => time(),
                                'status'       => 0,
                                'ip'           => ip2long($ip),
                            );
                $uid['u_id'] = 812345+$res;
                $map = array();
                $map['id'] = $res;
                D("User")->edit($map,$uid);
                $r = D("UserInfo")->add($info_m);

                if($r){
                    //验证码设为无效
                    $map=array();
                    $map['id'] = $_SESSION['code_id'];
                    $data['status'] = 1;
                    $re = D('Verification')->edit($map,$data);
                    if($re){
                        session('user',array(
                            'id'            => $res,
                            'user_name'     => $postdata['user_name'],
                            'nickname'      => $postdata['nickname'],
                            'url_img'       => $postdata['url_img'],
                            'user_phone'    => $postdata['user_phone'],
                            'user_email'    => $postdata['user_email'],
                            'realname'      => $postdata['realname'],
                            'idcard'        => $postdata['idcard'],
                            'card_status'   => $postdata['card_status'],
                            'u_id'   => $uid['u_id'],
                        ));

                        D('User')->commit();
                        D('Recommend')->commit();
                        $data = array('err'=>0,'msg'=>'注册成功！');
                        //發送註冊成功信息
                        R('Msg/sendmail',array($typename='regmsg',$userid = $res));
                        //給推手發送通知
                        if($referral){
                              R('Msg/sendmail',array($typename='newreg',$userid = $referral));
                        } 
                    }else{
                        D('User')->rollback();
                        D('Recommend')->rollback();//回滚
                        $data = array('err'=>5,'msg'=>'注册失败！');
                    }
                   
                }else{
                    D('User')->rollback();//回滚
                    D('Recommend')->rollback();//回滚
                    $data = array('err'=>2,'msg'=>'注册失败！');
                }
            }

            $this->ajaxReturn($data);

        }else{
            if(!$_SESSION['reg_phone']){
                header('Location:/Home/User/register_first');
            }
            $this->display();
        }
       
    }

    //验证注册
    

    //注册
    Public function check_register(){
        $referral = I('post.referral');
        if($referral){
             $postdata['referral'] = $referral;
             //添加投顾记录
            D('Recommend')->startTrans();
            $recommen['add_time'] = time();
            $recommen['user_id'] = $referral;
            D('Recommend')->add($recommen);
             //身份改为投顾
            $t_data['role'] = 2;
            $where['id'] = $referral;
            D("User")->edit($where,$t_data);
        }        
        $postdata['user_phone'] = I('post.phone');
        $code = I('post.code');
        $postdata['user_pass'] = I('post.password');
        $postdata['user_mixs'] = rand(111111,999999);
        $postdata['user_pass'] = md5($postdata['user_pass'].$postdata['user_mixs']);
        $postdata['user_name'] =rand(111,999).time();
        $postdata['nickname'] ="8号街".rand(1111,9999);
        $postdata['url_img'] =C('DOMAIN')."/App/Home/Public/images/default_photo.jpg";
        //查询验证码是否合法
        $map['phone'] = $postdata['user_phone'];
        $map['status'] = 0;
        $map['role'] = 1;
        $codeinfo = D('Verification')->get_one($map);
        if($codeinfo){
            if($codeinfo['code']==$code && $codeinfo['status']==0){
                if($codeinfo['time_failure']<=time()){

                    $data = array('err'=>1,'msg'=>'验证码已过期！');

                }else{

                    //验证码有效
                    D('User')->startTrans();
                    $res = D("User")->add($postdata);
                    if($res){
                        //用户信息拓展表
                        $ip = get_client_ip_user();
                                    $info_m = array(
                                        'id_user'      => $res,
                                        'form'         => 1,
                                        'invite_code'  => '',
                                        'joint_type'   => 0,
                                        'joint_id'     => '',
                                        'joint_token'  => '',
                                        'joint_val'    => '',
                                        'randpass'     => 0,
                                        'verify_phone' => 1,
                                        'verify_email' => 0,
                                        'time_last'    => time(),
                                        'time_add'     => time(),
                                        'status'       => 0,
                                        'ip'           => ip2long($ip),
                                    );

                        $uid['u_id'] = 812345+$res;
                        $map = array();
                        $map['id'] = $res;
                        D("User")->edit($map,$uid);
                          $r = D("UserInfo")->add($info_m);        
                        if($r){
                            //验证码设为无效
                            $map=array();
                            $map['id'] = $codeinfo['id'];
                            $data['status'] = 1;
                            $re = D('Verification')->edit($map,$data);
                            if($re){
                                  session('user',array(
                                    'id'            => $res,
                                    'user_name'     => $postdata['user_name'],
                                    'nickname'      => $postdata['nickname'],
                                    'url_img'       => $postdata['url_img'],
                                    'user_phone'    => $postdata['user_phone'],
                                    'user_email'    => $postdata['user_email'],
                                    'realname'      => $postdata['realname'],
                                    'idcard'        => $postdata['idcard'],
                                    'card_status'   => $postdata['card_status'],
                                    'u_id'   => $uid['u_id'],
                                ));

                                D('User')->commit();
                                $data = array('err'=>0,'msg'=>'注册成功！');
                                //發送註冊成功信息
                                R('Msg/sendmail',array($typename='regmsg',$userid = $res));
                                //給推手發送通知
                                if($referral){
                                      R('Msg/sendmail',array($typename='newreg',$userid = $referral));
                                } 
                            }else{
                                D('User')->rollback();
                                $data = array('err'=>5,'msg'=>'注册失败！');
                            }
                           
                        }else{
                            D('User')->rollback();//回滚
                            $data = array('err'=>0,'msg'=>'注册失败！');
                        }
                    }

                }
            }else{
                $data = array('err'=>3,'msg'=>'验证码错误！');
            }
        }else{
            $data = array('err'=>4,'msg'=>'验证码无效！');
        }
      $this->ajaxReturn($data);
    }

    //登录
    public function login() {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            if(IS_POST) {
            $phone = I('post.phone');
            $password = I('post.password');
            $map['user_phone'] = $phone;
            $data = D('User')->get_one($map);
            if($data) {
                if(md5($password . $data['user_mixs']) == ($data['user_pass'])){
                    //登录成功
                    session('user',array(
                                        'id'            => $data['id'],
                                        'user_name'     => $data['user_name'],
                                        'nickname'      => $data['nickname'],
                                        'url_img'       => $data['url_img'],
                                        'user_phone'    => $data['user_phone'],
                                        'realname'      => $data['realname'],
                                        'idcard'        => $data['idcard'],
                                        'card_status'   => $data['card_status'],
                                        'comment_status'   => $data['comment_status'],
                                        'bank_card'   => $data['bank_card'],
                                        'alipay'   => $data['alpay'],
                                        'u_id'   => $data['u_id'],
                                        'role'   => $data['role'],
                                        'referral'   => $data['referral'],
                                    ));
                    $map = array();
                    $map['user_id'] = $data['id'];
                    $data = array();
                    $data['time_last'] = time();
                    $userinfo = D('UserInfo')->edit($map,$data);
                    $data = array('err'=>0,'msg'=>"登录成功");
                }else{
                $data = array('err'=>2,'msg'=>"登录密码有误，请重新输入");
                }
            } else {
                $data = array('err'=>1,'msg'=>"该手机号码尚未注册");
            }
            $this->ajaxReturn($data);
        }
        $this->display();
        } else {
			$callback = I('get.callback');
            header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa5913a5be38361dd&redirect_uri=http%3a%2f%2fwei.8hjie.com%2fHome%2fUser%2fwechat_login%3fcallback%3d'.$callback.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
        }

        
    }
	
	//微信登錄
	public function wechat_login(){
		$callback = I('get.callback');
		$code = I('get.code');
		if($code){
			$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxa5913a5be38361dd&secret=d1bfe116b7c850f028f64cfc039e1836&code='.$code.'&grant_type=authorization_code';
			$r=$this->juhecurl($url);
		//print_r($r);die;
			if($r['unionid']){
				//成功拉取微信號信息
				//查詢用戶信息
				$unionid = $r['unionid'];
				$map['joint_id'] = $unionid;
				$user_info = D('UserInfo')->get_one($map);
				
				if($user_info){
					$userid = $user_info['id_user'];
					$map = array();
					$map['id'] = $userid;
					$data = D('User')->get_one($map);
					if(!$data){
						$map = array();
						$map['joint_id'] = $unionid;
						D('UserInfo')->where($map)->delete();
						$user_info=false;
					} 
				}
				
				
					
				//print_r($user_info);die;
				if($user_info){
					//該微信已註冊直接查詢用戶表
					$userid = $user_info['id_user'];
					$map = array();
					$map['id'] = $userid;
					$data = D('User')->get_one($map);
					$map = array();
					$map['id_user'] = $userid;
					$info_data = D('UserInfo')->get_one($map);
					
					//登錄成功
					 session('wechat',array(
								'id'=> $data['id'],
								'headimgurl'=> $data['url_img'],
								'nickname'=> $data['nickname'],
								'joint_id'=> $info_data["joint_id"],
								'joint_token'=> $info_data["joint_token"],
								'joint_val'=> $info_data["joint_val"],
							));	
					session('wechat_user_info',$user_info);
					//判斷用戶是否有手機號碼
					if($data['user_phone']){
						$map = array();
						$map['user_id'] = $data['id'];
						$datas = array();
						$datas['time_last'] = time();
						$userinfo = D('UserInfo')->edit($map,$datas);
						 session('user',array(
                                        'id'            => $data['id'],
                                        'user_name'     => $data['user_name'],
                                        'nickname'      => $data['nickname'],
                                        'url_img'       => $data['url_img'],
                                        'user_phone'    => $data['user_phone'],
                                        'realname'      => $data['realname'],
                                        'idcard'        => $data['idcard'],
                                        'card_status'   => $data['card_status'],
                                        'comment_status'   => $data['comment_status'],
                                        'bank_card'   => $data['bank_card'],
                                        'alipay'   => $data['alpay'],
                                        'u_id'   => $data['u_id'],
										'referral'   => $data['referral'],
                                        'role'   => $data['role'] 
                                    ));
						//print_r($_SESSION);	die;
						//header('Location:/Home/Person/index'); 
						if($callback){
							header('Location:'.$callback); 
						}else{
							header('Location:/Home/Person/index'); 
						}
						
					}else{
						
						header('Location:/Home/User/msg_ssid');	
					}
				}else{
					//mei有用户 注册微信
					$url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wxa5913a5be38361dd&grant_type=refresh_token&refresh_token='.$r['refresh_token'];
						$res=$this->juhecurl($url);	
						if($res['access_token']){
							$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$res['access_token'].'&openid='.$res['openid'].'&lang=zh_CN';
							$res_info=$this->juhecurl($url);	
						}
						
						$mixs  = rand(100000,999999);
						$password   = md5(md5(rand(100000,999999)) . $mixs);
						$mixs_name  = 'wx' . rand(100000,999999) . time();
									
						 $insert_user = array(
								'user_name'     => $mixs_name,//用户名
								'user_phone'    => '',//手机
								'user_email'    => '',//邮箱
								'user_pass'     => $password,//密码
								'user_mixs'     => $mixs,//混淆
								'url_img'       => $res_info['headimgurl'],//图片
								'nickname'      => $res_info["nickname"],//昵称
							);
							
						$newuserid = D('User')->add($insert_user);
						 $joint_val = array(
							'openid'        => $res['openid'],
							'refresh_token' => $r['refresh_token'],
						);	
						$insert_user_info = array(
							'id_user'       => $newuserid, //user.id
							'form'          => '1', //0默认,1PC,2IOS,3ANDROID
							'invite_code'   => '', //邀请码
							'joint_type'    => '3', //登录类型:0网站注册,1QQ,2微博,3微信
							'joint_id'      => $res_info['unionid'], //联合登录用户id
							'joint_token'   => $res['access_token'], //联合登录token(重要)
							'joint_val'     => json_encode(json_encode($joint_val)), //联合登录值(备用)
							'randpass'      => '1', //状态:0自定义,1默认密码
							'verify_phone'  => '0', //验证0未验证1验证
							'verify_email'  => '0', //验证0未验证1验证
							'time_last'     => time(), //最后登录时间
							'time_add'      => time(), //注册时间
							'status'        => '0', //状态:0启用1禁用
							'ip'            => get_client_ip_user(),
						);
						 session('wechat',array(
								'id'=> $newuserid,
								'headimgurl'=> $res_info['headimgurl'],
								'nickname'=> $res_info["nickname"],
							));	
//print_r($_SESSION);die;							
						D('UserInfo')->add($insert_user_info);
						session('wechat_user',$insert_user);	
						session('wechat_user_info',$insert_user_info);
						header('Location:/Home/User/msg_ssid');							
						
					
				}
			}  
		}
		
	}
	//微信注册绑定手机号码
	public function wechat_regist(){
		//print_r($_SESSION);die;
		if(IS_POST){
			$phone = I('post.phone');
            $code = I('post.code');
            $map['phone'] = $phone;
            $map['status'] = 0;
            $map['role'] = 1;
            $codeinfo = D('Verification')->get_one($map);
            if($codeinfo){
				//var_dump($codeinfo['code']);
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){
                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                    }else{
						$unionid = $_SESSION['wechat_user_info']['joint_id'];
						$map['joint_id'] = $unionid;
						$user_info = D('UserInfo')->get_one($map);
                        if(!$user_info && $_SESSION['wechat_user_info']['id_user']){
                            $_SESSION['wechat_user_info']['id_user'] = $_SESSION['wechat_user_info'];
                            $add_r =  D('UserInfo')->add($insert_user_info);
                            if($add_r){
                                $map = array();
                                $map['id_user'] =  $_SESSION['wechat_user_info']['id_user'];
                                $user_info = D('UserInfo')->get_one($map);
                            }elseif(!$_SESSION['wechat_user_info']['id_user']){
                                $data = array('err'=>5,'msg'=>'绑定失败008！');
                                $this->ajaxReturn($data);
                            }else{
                                $data = array('err'=>5,'msg'=>'绑定失败007！');
                                $this->ajaxReturn($data);
                            }

                        }else if($user_info && !$_SESSION['wechat_user_info']['id_user']){
							$map = array();
                            $map['id_user'] =  $_SESSION['wechat_user_info']['id_user'];
                            $user_info = D('UserInfo')->get_one($map);
							if(!$user_info){
								$data = array('err'=>5,'msg'=>'绑定失败010！');
								$this->ajaxReturn($data);
							}
						}else{
                            $map = array();
                            $map['id_user'] =  $_SESSION['wechat_user_info']['id_user'];
                            $user_info = D('UserInfo')->get_one($map);
							if(!$user_info){
								$data = array('err'=>5,'msg'=>'绑定失败009！');
								$this->ajaxReturn($data);
							}
							
                        }
						//print_r($_SESSION);die;
						$password = I('post.password');
						$postdata['user_phone'] = $phone;
						$postdata['user_pass'] = I('post.password');
						$postdata['user_mixs'] = rand(111111,999999);
						$postdata['user_pass'] = md5($password.$postdata['user_mixs']);
						$postdata['user_name'] ='wxbd'.rand(111,999).time();
						$postdata['nickname'] =$_SESSION['wechat']['nickname'];
						$postdata['url_img'] =$_SESSION['wechat']['headimgurl'];
						//print_r($_SESSION);die;

					D('User')->startTrans();//開啟事物
					$map = array();
					$map['id'] = $user_info['id_user'];
					$res = D("User")->edit($map,$postdata);
				//echo D("User")->_sql();
						if($res){
								//用户信息拓展表
								$ip = get_client_ip_user();
								$info_m = array(
									'form'         => 1,
									'invite_code'  => '',
									'joint_type'   => 3,
									'joint_id'     => $_SESSION['wechat_user_info']['joint_id'],
									'joint_token'  => $_SESSION['wechat_user_info']['joint_token'],
									'joint_val'    => $_SESSION['wechat_user_info']['joint_val'],
									'randpass'     => 0,
									'verify_phone' => 1,
									'verify_email' => 0,
									'time_last'    => time(),
									'time_add'     => time(),
									'status'       => 0,
									'ip'           => ip2long($ip),
								);
							$map = array();
							$map['id'] = $user_info['id'];
							$r = D("UserInfo")->edit($map,$info_m);
						//echo D("UserInfo")->_sql();die; 
							if($r){
								//验证码设为无效
								$map=array();
								$map['id'] = $codeinfo['id'];
								$data['status'] = 1;
								$re = D('Verification')->edit($map,$data);
								if($re){
									session('user',array(
										'id'            => $user_info['id_user'],
										'user_name'     => $postdata['user_name'],
										'nickname'      => $postdata['nickname'],
										'url_img'       => $postdata['url_img'],
										'user_phone'    => $postdata['user_phone'],
										'user_email'    => $postdata['user_email'],
										'realname'      => $postdata['realname'],
										'idcard'        => $postdata['idcard'],
										'card_status'   => $postdata['card_status'],
										'u_id'   => $uid['u_id'],
									));

									D('User')->commit();
									$data = array('err'=>0,'msg'=>'绑定成功！');
									unset($_SESSION['wechat_user']);
									unset($_SESSION['wechat_user_info']);
									//發送註冊成功信息
									R('Msg/sendmail',array($typename='regmsg',$userid = $res));
									//給推手發送通知
										if($referral){
											  R('Msg/sendmail',array($typename='newreg',$userid = $referral));
										} 
										
								}else{
									D('User')->rollback();
									$data = array('err'=>5,'msg'=>'绑定失败！');
								}
					   
							}else{
								D('User')->rollback();//回滚
								$data = array('err'=>2,'msg'=>'绑定失败002！');
							}
						}else{
							unset($_SESSION);
							$data = array('err'=>5,'msg'=>'绑定失败005！');
						}
			
                    }
                }else{
                    $data = array('err'=>3,'msg'=>'验证码错误！');
                }
            }else{
                    $data = array('err'=>2,'msg'=>'验证码无效！');
            }
			
		$this->ajaxReturn($data);
		}else{
			//print_r($_SESSION);
			$this->display();
		}
		
	}
	//微信登錄沒有手機號碼提示跳轉頁
	public function msg_ssid(){
		//print_r($_SESSION);die;
		if(IS_POST){
			$phone = I('post.phone');
			$password = I('post.password');
			
			$map['user_phone'] = $phone;
            $data = D('User')->get_one($map);
            if($data) {
                if(md5($password . $data['user_mixs']) == ($data['user_pass'])){
					$userid = $_SESSION['wechat']['id'];
					//合并userinfo
					$map = array();
					$map['id_user'] = $userid;
					$wx_user_info = D('UserInfo')->get_one($map);
					$arr['joint_id'] = $wx_user_info['joint_id'];
					$arr['joint_token'] = $wx_user_info['joint_token'];
					$arr['joint_val'] = $wx_user_info['joint_val'];
					//修改至绑定的数据
					 D('UserInfo')->startTrans();//开启事物
					$map = array();
					$map['id_user'] = $data['id'];
					$user_info_r = D('UserInfo')->edit($map,$arr);
					if($user_info_r){
						//删除用户与用户信息表
						$map = array();
						$map['id'] = $userid;
						$d_user = M('User')->where($map)->delete();
						$map = array();
						$map['id_user'] = $userid;
						$d_user_info = M('UserInfo')->where($map)->delete();
						if($d_user && $d_user_info){
							D('UserInfo')->commit();
							D('User')->commit();
						}else{
							D('UserInfo')->rollback();
							D('User')->rollback();
						}
					}
					unset($_SESSION['user']);
                    //登录成功
                    session('user',array(
                                        'id'            => $data['id'],
                                        'user_name'     => $data['user_name'],
                                        'nickname'      => $data['nickname'],
                                        'url_img'       => $data['url_img'],
                                        'user_phone'    => $data['user_phone'],
                                        'realname'      => $data['realname'],
                                        'idcard'        => $data['idcard'],
                                        'card_status'   => $data['card_status'],
                                        'comment_status'   => $data['comment_status'],
                                        'bank_card'   => $data['bank_card'],
                                        'alipay'   => $data['alpay'],
                                        'u_id'   => $data['u_id'],
                                        'role'   => $data['role']
                                    ));
                    $map = array();
                    $map['user_id'] = $data['id'];
                    $data = array();
                    $data['time_last'] = time();
                    $userinfo = D('UserInfo')->edit($map,$data);
					
                    $data = array('err'=>0,'msg'=>"绑定成功！");
                }else{
				unset($_SESSION['user']);
                $data = array('err'=>2,'msg'=>"密码有误，请重新输入");
                }
            } else {
                $data = array('err'=>1,'msg'=>"该手机号码尚未注册");
            }
			$this->ajaxReturn($data);
		}else{
			$this->display();
		}
		
	}
	//微信登錄綁定舊賬號
	
	
	//忘記登錄密碼
    public function forgot_password(){
        if(IS_POST){
            $postdata['user_phone'] = I('post.phone');
            $code = I('post.code');
            $postdata['user_pass'] = I('post.password');
            $postdata['user_mixs'] = rand(111111,999999);
            $postdata['user_pass'] = md5($postdata['user_pass'].$postdata['user_mixs']);
         //查询验证码是否合法
            $map['phone'] = $postdata['user_phone'];
            $map['status'] = 0;
            $map['role'] = 3;
            $codeinfo = D('Verification')->get_one($map);
            if($codeinfo){
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){
                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                    }else{
                        D('User')->startTrans();
                        //验证码有效
                        $map = array();
                        $map['user_phone'] = $postdata['user_phone'];
                        $data['user_mixs'] = $postdata['user_mixs'];
                        $data['user_pass'] = $postdata['user_pass'];
                        $res = D('User')->edit($map,$data);

                        if($res){
                                //验证码设为无效
                                $map=array();
                                $data = array();
                                $map['id'] = $codeinfo['id'];

                                $data['status'] = 1;
                                $re = D('Verification')->edit($map,$data);
                                if($re){
                                     D('User')->commit();
                                    $data = array('err'=>0,'msg'=>'修改成功！');
                                }else{
                                    D('User')->rollback();
                                    $data = array('err'=>5,'msg'=>'修改失败！');
                                }
                        }

                    }
            }else{
                $data = array('err'=>3,'msg'=>'验证码错误！');
            }
        }else{
            $data = array('err'=>4,'msg'=>'验证码无效！');
        }
        $this->ajaxReturn($data);
        }else{
            $this->display();
        }
    }
    //实名
    public function realname($realname,$idcard,$val=0){
        if(!$realname && !$idcard){
            $realname = I('post.realname');
            $idcard = I('post.idcard');
        }
        
        $map['id'] = $_SESSION['user']['id'];
        $user = D('User')->get_one($map);
        if($user['idcards']) {
            $data = array(
                    'err' => 8,
                    'msg' => "您已实名认证"
                );
                if($val){
                 return $data;
                }else{
                 $this->ajaxReturn($data);
                }
        }
        $map = array();
        $map['idcard'] = $idcard;
        $idcards = D('User')->get_one($map);
        if($idcards) {
            $data = array(
                    'err' => 8,
                    'msg' => "身份证已被绑定"
                );
                if($val){
                 return $data;
                }else{
                 $this->ajaxReturn($data);
                }
        }


         if(empty($realname)) {
            $data = array(
                'err' => 7,
                'msg' => "名字不能为空"
            );
            if($val){
                 return $data;
            }else{
                 $this->ajaxReturn($data);
            }
        }
        if(empty($idcard)) {
            $data = array(
                'err' => 8,
                'msg' => "身份证号码不能为空"
            );
            if($val){
                 return $data;
            }else{
                 $this->ajaxReturn($data);
            }
        }
        $map['user_id'] = $_SESSION['user']['id'];
        $re = M('Realname')->where($map)->find();
        if($re){
                $add_day = date('ymd',$re['add_time']);
                $num = $re['num'];
                $today = date('ymd',time());
                    if($num>=5){
                        if($today==$add_day){
                            $data = array(
                                'err' => 4,
                                'msg' => "今天验证次数已经超过5次！明天再来！"
                            );
                        }else{
                            //验证
                            $data =$this->get_real($idcard,$realname);
                            if($data['err']!=3){
                                $arr_data = array('num'=>1,'add_time'=>time());
                                $map['id'] = $re['id'];
                                $r = M('Realname')->where($re['id'])->save($arr_data);
                            }
                            
                        }
                    }else{
                            $num = $num+1;
                            $number = 5-$num;
                            $data =$this->get_real($idcard,$realname,$number);
                            if($data['err']!=3){
                                $arr_data = array('num'=>$num,'add_time'=>time());
                                $map['id'] = $re['id'];
                                $r = M('Realname')->where($re['id'])->save($arr_data);
                            }
                            
                    }

        }else{
                        $data = $this->get_real($idcard,$realname);
                        if($data['err']!=3){
                            $arr_data = array('num'=>1,'add_time'=>time(),'user_id'=>$_SESSION['user']['id']);
                            $r = M('Realname')->data($arr_data)->add();
                        }
        }
        if($val){
            return $data;
        }else{
            $this->ajaxReturn($data);
        }

    }



    public function get_real($idcard,$realname,$number='4'){
     $real =R('Real/v_real',array($idcard,$realname));
   // $real =Array ( 'reason' => '成功', 'result' => Array ( 'realname' => '王华斌', 'idcard'=>'362427199205230819', 'res'=> 1 ), 'error_code'=> 0 );
    //    $real = '{"reason":"\u6210\u529f","result":{"realname":"\u738b\u534e\u658c","idcard":"362427199205230819","res":1},"error_code":0}';
     
        if($real){
            if($real['error_code']=='0') {

               if($real['result']['res']==1){
                $data = array(
                    'err' => 1,
                    'msg' => "验证成功！"
                );
                //写入用户数据
                $id = $_SESSION['user']['id'];
                $map['id'] = $id;
                $arr_data = array(
                    'realname' => $realname,
                    'idcard'   => $idcard,
                    'card_status' => 1
                );
                $user =D('User')->edit($map,$arr_data);
                //實名成功發送消息
                R('Msg/sendmail',array($typename='realname',$userid = $id));
                $_SESSION['user']['realname'] = $realname;
                $_SESSION['user']['idcard'] = $idcard;
                $_SESSION['user']['card_status'] = 1;
               }else{
                     $data = array(
                        'err' => 2,
                        'msg' => "验证失败，还剩".$number."次机会"
                    );
               }
            } else {
                $data = array(
                    'err' => 3,
                    'msg' =>$real['reason']
                );
            }
        }else{
                    $data = array(
                        'err' => 3,
                        'msg' => "验证请求失败！"
                    );
        }
    return($data);


    }
	
	public function user_agreement(){
        $this->display();
    }
	
	
	private function juhecurl($params = '') {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_URL, $params);
            $json =  curl_exec($ch);
            curl_close($ch);
            $arr=json_decode($json,1);
            return $arr;
    
	}




    public function send_sms_reg(){
    //    $code = I('post.code_img');
    //    $result = $this->check_verify($code);
        //var_dump($a);die;
    //    if($result){
            $phone = I('post.phone');
             if($phone){
                
                //查询用户是否可以发送短信
                $map['phone'] = $phone;
                $map['status'] =0;
                $map['role'] =1;
               
                $data = D("Verification")->get_one($map);
                $startday = strtotime(date('Y-m-d 0:0:0',time()));
                $where['add_time'] = array('gt',$startday);
                $where['phone'] = $phone;
                $msgcount = C('MSG_COUNT_USER');
                $ip = get_client_ip_user();

                 $count = D("Verification")->count($where);
                if($count>=$msgcount){
                    $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                    $this->ajaxReturn($data);
                }else{
                    $where = array();
                    $where['add_time'] = array('gt',$startday);
                    $where['phone'] = $phone;
                    $where['ip'] = $ip;
                    $count = D("Verification")->count($where);
                    if($count>=$msgcount){
                        $data = array('err'=>3,'msg'=>"当前ip发送次数过多！");
                        $this->ajaxReturn($data);
                    }else{
                        if($data['add_time']<=(time()-60)){
                            //发送验证码
                            $code = rand(111111,999999);
                           $res = R('Send/sendsms',array($phone,$code,$type = 1));
                           //$res = array('err'=>0);
                            if($res['err']==0){
                                //记录短信发送
                                $msgdata['type']=1;
                                $msgdata['role']=1;
                                $msgdata['phone']=$phone;
                                $msgdata['status']=0;
                                $msgdata['add_time']=time();
                                $msgdata['time_failure']=time()+60;
                                $msgdata['code']=$code;
                                $msgdata['ip']=$ip;
                                $res = D("Verification")->add($msgdata);
                                if($res){
                                    $data = array('err'=>0,'msg'=>"发送成功！");
                                    $this->ajaxReturn($data);
                                }else{
                                    $data = array('err'=>5,'msg'=>"发送失败！");
                                    $this->ajaxReturn($data);
                                }

                            }else{

                                $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                $this->ajaxReturn($data);

                            }
                        }else{
                            $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                            $this->ajaxReturn($data);
                        }
                    }
                }
             }

        //}else{
        //    $data = array('err'=>1,'msg'=>"图形验证码错误！");
        //    $this->ajaxReturn($data);
        //}
		
	
    }
    public function no_code_send(){
            $phone = I('post.phone');
            
             if($phone){
                
                //查询用户是否可以发送短信
                $map['phone'] = $phone;
                $map['status'] =0;
                 $map['role'] =1;
               
                $data = D("Verification")->get_one($map);
                $startday = strtotime(date('Y-m-d 0:0:0',time()));
                $where['add_time'] = array('gt',$startday);
                $where['phone'] = $phone;
                $msgcount = C('MSG_COUNT_USER');
                $ip = get_client_ip_user();

                 $count = D("Verification")->count($where);
                if($count>=$msgcount){
                    $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                    $this->ajaxReturn($data);
                }else{
                    $where = array();
                    $where['add_time'] = array('gt',$startday);
                    $where['phone'] = $phone;
                    $where['ip'] = $ip;
                    $count = D("Verification")->count($where);
                    if($count>=$msgcount){
                        $data = array('err'=>3,'msg'=>"当前ip发送次数过多！");
                        $this->ajaxReturn($data);
                    }else{
                        if($data['add_time']<=(time()-60)){ 
                            //发送验证码
                            $code = rand(111111,999999); 
                           $res = R('Send/sendsms',array($phone,$code,$type = 1));
                           //$res = array('err'=>0);
                            if($res['err']==0){
                                //记录短信发送
                                $msgdata['type']=1;
                                $msgdata['role']=1;
                                $msgdata['phone']=$phone;
                                $msgdata['status']=0;
                                $msgdata['add_time']=time();
                                $msgdata['time_failure']=time()+300;
                                $msgdata['code']=$code;
                                $msgdata['ip']=$ip;
                                $res = D("Verification")->add($msgdata);

                                if($res){
                                    $data = array('err'=>0,'msg'=>"发送成功！");
                                    $this->ajaxReturn($data);
                                }else{
                                    $data = array('err'=>5,'msg'=>"发送失败！");
                                    $this->ajaxReturn($data);
                                }

                            }else{

                                $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                $this->ajaxReturn($data);

                            }
                        }else{
                            $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                            $this->ajaxReturn($data);
                        }
                    }
                }
             }
    }

	
	
	  //注册
    public function fast_register(){
               
        $postdata['user_phone'] = I('post.phone');
        $id = I('post.id');
        if(!$id){
            $data = array('err'=>1,'msg'=>"该营销页有误！");
            $this->ajaxReturn($data);
        }
       // echo 1;die;
        $map['user_phone'] = $postdata['user_phone'];
        
        $r = D('User')->get_one($map);
        if($r){
            if($r['is_online']==0){
                $data = array('err'=>1,'msg'=>"该号已通过别的渠道注册了！");
                $this->ajaxReturn($data);
            }else{
                $phone =$postdata['user_phone'];
                $realname ="";
                $idcard ="";
                $userid =$r['id'];
            }
            
        }else{
            $code = I('post.code');
            $pass = md5(rand(111111,999999));
            $postdata['user_pass'] =$pass;
            $postdata['user_mixs'] = rand(111111,999999);
            $postdata['user_pass'] = md5($postdata['user_pass'].$postdata['user_mixs']);
            $postdata['user_name'] =rand(111,999).time();
            $postdata['nickname'] ="8号街".rand(1111,9999);
            $postdata['url_img'] =C('DOMAIN')."/App/Home/Public/images/default_photo.jpg";
            $postdata['is_online'] =1;
            $postdata['referral']=$id; 
           

            $map['phone'] = $postdata['user_phone'];
            $map['status'] = 0;
            $map['role'] = 1;
            $codeinfo = D('Verification')->get_one($map);

            if($codeinfo){
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){

                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                         $this->ajaxReturn($data);

                    }else{
                        D('User')->startTrans();
                        $res = D("User")->add($postdata);
                         //用户信息拓展表
                        $ip = get_client_ip_user();
                         $info_m = array(
                            'id_user'      => $res,
                            'form'         => 1,
                            'invite_code'  => '',
                            'joint_type'   => 0,
                            'joint_id'     => '',
                            'joint_token'  => '',
                            'joint_val'    => '',
                            'randpass'     => 0,
                            'verify_phone' => 1,
                            'verify_email' => 0,
                            'time_last'    => time(),
                            'time_add'     => time(),
                            'status'       => 0,
                            'ip'           => ip2long($ip),
                        );
                        $uid['u_id'] = 812345+$res;
                        $map = array();
                        $map['id'] = $res;
                        D("User")->edit($map,$uid);
                        $r = D("UserInfo")->add($info_m); 
                        D('User')->commit();  
                        $phone =$postdata['user_phone'];
                        $realname ="";
                        $idcard ="";
                        $userid =$res;

                    }
                }else{
                    $data = array('err'=>4,'msg'=>'验证码错误！');
                     $this->ajaxReturn($data);
                }
            }else{
                    $data = array('err'=>4,'msg'=>'验证码无效！');
                     $this->ajaxReturn($data);
            }
        }

        if($userid){
			$tag = I('post.tag');
			if($tag=='phyt'){
				import("Api.Puhuiyuntong");
				$ph = new \Puhuiyuntong();
				$info = $ph->login();
				$login_info = json_decode($info,true);
				
				$token = $login_info['data']['token'];
				$r = $ph->bind_user($token,$phone,urlencode($nickname),$idcard,$userid);
			
				$r = json_decode($r,true);
				if($r['Status']==200 or $r['Status']==202 or $r['Status']=="1003" or $r['Status']=="201"){
					$map = array();
					$map['user_id'] = $userid;
					$map['platform_id'] =1;
					$u_plat = D('UserPlatform')->where($map)->find();
					if(!$u_plat && $userid){
						$map = array();
						$map['id'] = $userid;
						$user_info = D('User')->where($map)->find();

						$data['user_id'] = $userid;
						$data['add_time'] = time();
						$data['platform_id'] = 2;
						$data['referral'] =$user_info['referral'];
						D('UserPlatform')->add($data);
					}

					$data = array('phone'=>$phone,'nickname'=>$nickname,'idcard'=>$idcard,'UserId'=>$userid);
					$rsa_data = $this->rsa_pubencrypt($data);
					$info = $ph->login();
					$login_info = json_decode($info,true);
					$token = $login_info['data']['token'];
					$url = 'https://www.phyt88.com/v3/finance/dologin?token='.$token.'&datakey=login';
				   //$url="http://new.8hjie.com/Home/Platform/test";
					 if($r['Status']==200 or $r['Status']==202 or $r['Status']=='1003'){
						$data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
					}elseif($r['Status']=='201'){
						$data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
					}
				}
		
				
			}else{
				 //跳转红岭
					import("Api.Hongling");
					$hl = new \Hongling();
					$info = $hl->bind_user($phone,urlencode($realname),$idcard,$userid);
					$info = json_decode($info,true);
					if($info['status']==200){
							$consumerId ='17111500001';
							$time = date('YmdHis',time());

							$data = array('time'=>$time,'phone'=>$phone,'UserId'=>$userid);
							$rsa_data = $this->rsa_pubencrypt($data);
						   
							$sign = "430e66a9244e36058f8acfcf68e5c5c2&consumerId=".$consumerId."&data=".$rsa_data;
							$sign = md5($sign);
							$url = 'https://sso.my089.com/sso/8hjrj/login?consumerId='.$consumerId.'&data='.$rsa_data.'&sign='.$sign;
							$data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
					}else{
						 $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
					}
			}
					
        }
      $this->ajaxReturn($data);
    }
	
	public function rsa_pubencrypt($data){
        $id = I('post.id');
        import("Api.Rsa"); 
        $data = json_encode($data);
        ////////////////////////////////////// 
        $pubKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_public_key.pem'; 
        $privKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_private_key.pem';
        // $m = new RSA($pubfile, $prifile); 
        $m = new \Rsa($pubKey, $privKey);
        $x = $m->pubEncrypt($data); 
        return($x);
       
    }
	
	/*
    //注册
    public function fast_register(){
               
        $postdata['user_phone'] = I('post.phone');
        $id = I('post.id');
        //echo 1;die;
        $map['user_phone'] = $postdata['user_phone'];
        $r = D('User')->get_one($map);
        if($r){
            if($r['is_online']==0){
                $data = array('err'=>1,'msg'=>"该号已通过别的渠道注册了！");
                $this->ajaxReturn($data);
            }else{
                $phone =$postdata['user_phone'];
                $realname ="";
                $idcard ="";
                $userid =$r['id'];
            }
            
        }else{
            $pass = md5(rand(111111,999999));
            $postdata['user_pass'] =$pass;
            $postdata['user_mixs'] = rand(111111,999999);
            $postdata['user_pass'] = md5($postdata['user_pass'].$postdata['user_mixs']);
            $postdata['user_name'] =rand(111,999).time();
            $postdata['nickname'] ="8号街".rand(1111,9999);
            $postdata['url_img'] =C('DOMAIN')."/App/Home/Public/images/default_photo.jpg";
            $postdata['is_online'] =1;
            $postdata['referral'] =$id;
            D('User')->startTrans();
            $res = D("User")->add($postdata);
                    
            //用户信息拓展表
            $ip = get_client_ip_user();
                        $info_m = array(
                            'id_user'      => $res,
                            'form'         => 1,
                            'invite_code'  => '',
                            'joint_type'   => 0,
                            'joint_id'     => '',
                            'joint_token'  => '',
                            'joint_val'    => '',
                            'randpass'     => 0,
                            'verify_phone' => 1,
                            'verify_email' => 0,
                            'time_last'    => time(),
                            'time_add'     => time(),
                            'status'       => 0,
                            'ip'           => ip2long($ip),
                        );
            $uid['u_id'] = 812345+$res;
            $map = array();
            $map['id'] = $res;
            D("User")->edit($map,$uid);
            $r = D("UserInfo")->add($info_m); 
            D('User')->commit();  
            $phone =$postdata['user_phone'];
            $realname ="";
            $idcard ="";
            $userid =$res;

        }

        
        //跳转红岭
        import("Api.Hongling");
        $hl = new \Hongling();
        $info = $hl->bind_user($phone,urlencode($realname),$idcard,$userid);
        $info = json_decode($info,true);
        if($info['status']==200){
             $consumerId ='17111500001';
                $time = date('YmdHis',time());

                $data = array('time'=>$time,'phone'=>$phone,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);
               
                $sign = "430e66a9244e36058f8acfcf68e5c5c2&consumerId=".$consumerId."&data=".$rsa_data;
                $sign = md5($sign);
                $url =  'http://dev-sso.my089.net/sso/8hjrj/login?consumerId='.$consumerId.'&data='.$rsa_data.'&sign='.$sign;
                $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
        }else{
             $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
        }
      $this->ajaxReturn($data);
    }
	*/
	public function loginout(){
        session(null);
        header('Location:/');
    }


	
    //选择圈子
    public function circle(){
        //echo 1;die;
        //平台列表
        $map['status']=1;
		$map['id'] = array('neq',4);
        $list = D('Circle')->get_all($map,$field='*');
        $map = array();
        foreach ($list as $k => $v) {
            //加入圈子人数
            $map['platform_id'] = $v['platform_id'];
            $group = M('ImGroup')->where($map)->find();
            $map = array();
            $map['group_id'] = $group['groupid'];
            $map['status'] = 0;
            $group_count = M('ImGroupUser')->where($map)->count();
            $list[$k]['group_count'] = $group_count;
             //深度评测
            $map = array();
            $map['platform_id'] = $v['platform_id'];
            $depth_count = M('OtthAppraisal')->where($map)->count();  //深度评测条数
            $list[$k]['depth_count'] = $depth_count;
            //热门点评
            $commen_count = M('PlatformCommentary')->where($map)->count();//短评条数
            $annou_count = M('CircleAnnounce')->where($map)->count();//信批评论条数
            $opin_count = M('CircleOpinion')->where($map)->count();//舆情条数
            $total_count = $depth_count+$commen_count+$annou_count+$opin_count;//总的热评条数
            $list[$k]['total_count'] = $total_count;
            //区分热门
             $list[$k]['cricel_leve']=$this->get_cricel_leve($v['platform_id']);

             //查询投顾
             $maps = array();
             $maps['push_platform_id'] = $v['platform_id'];
             $push_user= D('Recommend')->get_one($maps);
             //echo D('Recommend')->_sql();
             $maps = array();
             $maps['id'] = $push_user['user_id'];
             $push_user['user'] = D('User')->get_one($maps);
             $list[$k]['push_user'] =$push_user;
         }
         //投顾列表
        $map = array();
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user = D('User')->get_one($map);
        if($user){
            $map = array();
            $map['user_id'] = $user['id'];
            $map['status'] = 1;
            $fans_info =  M('AdviserFollow')->where($map)->field('recommend_id')->select();

            if($fans_info){
                foreach($fans_info as $k=>$v){
                    $fans_list[]=$v['recommend_id'];
                }
            }
        }


        $map = array();
        $push_list = D('RecommendPlatform')->where($map)->group('recommend_id')->select();
        foreach ($push_list as $k => $v) {
            $push_list[$k]['user_info'] = $user['nickname'];
            $recommend_id = $v['recommend_id'];
            $map['id'] = $recommend_id;
            $recommen= D('Recommend')->get_one($map);
            if(in_array($recommen['id'],$fans_list)){
                $push_list[$k]['is_fans'] = 1;
            }else{
                $push_list[$k]['is_fans'] = 0;
            }
            $platform_id_list = explode(',', $recommen['push_plat_id']);
           // print_r()
            foreach ($platform_id_list as $ke => $va) {
                 $map['id'] = $va;
                 $plat_info = D('Platform')->get_one($map);
                 $push_list[$k]['platform_id_list_arr'][] = $plat_info['name'];
            }
            $push_list[$k]['platform_id_list'] = implode(',', $push_list[$k]['platform_id_list_arr']);
            $map = array();
            $map['referral'] = $recommen['user_id'];
            $push_list[$k]['info'] = $recommen;
            $referral= D('User')->count($map);
            $push_list[$k]['referral'] = $referral;
            $map = array();
            $map['id'] = $recommen['user_id'];
            $push_list[$k]['user']= D('User')->get_one($map);

        }

        $sort = array(
        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
        'field'     => 'referral',       //排序字段
        );

         $arrSort = array();
        foreach($push_list AS $uniqid => $row){
            foreach($row AS $key=>$value){
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if($sort['direction']){
            array_multisort($arrSort[$sort['field']],constant($sort['direction']),$push_list);
        }

        foreach ($push_list as $k => $v) {

            $recommend_id = $v['recommend_id'];
            $arr[$recommend_id] = $push_list[$k];

        }
        $push_list = array_slice($push_list,0,10);
        //print_r($list);
        $this->assign('push_list',$push_list);
        $this->assign('list',$list);
        $this->display();
    }

	

    /**
     * [get_cricel_leve 获取平台评级数据]
     * @param  [type] $platform_id [平台ID]
     * @return [type]              [array]
     */
    public function get_cricel_leve($platform_id){
        $map['platform_id'] = $platform_id;
        $list = M('PlatformGrade')->where($map)->field('count(*) as count,user_role,colligate')->group('user_role,colligate')->order('count(*) desc,colligate asc')->select();
        $list_all = M('PlatformGrade')->where($map)->field('count(*) as count,user_role,colligate')->group('colligate')->order('count(*) desc,colligate asc')->select();
        $count_user = 0;
        $count = 0;
        $count_push = 0;
       // return $list_all;
        foreach ($list_all as $k => $v) {
            $count = $count+$v['count'];
            $all_grade['all_count']=$count;
            $all_grade[]=$v;

        }
        foreach($list as $k=>$v){
            $role = $v['user_role'];

            if($role==1){
                $count_user = $count_user+$v['count'];
                $usre_grade['all_count']=$count_user;
                $usre_grade[]=$v;
            }else{
                $count_push = $count_push+$v['count'];
                $usre_push['all_count']=$count_push;
                $usre_push[]=$v;
            }
        }
        
        foreach ($usre_grade as $k => $v) {
           $count_percent = sprintf("%.0f",$v['count']/$usre_grade['all_count']*100);
           $usre_grade[$k]['count_percent']=$count_percent;
           $usre_grade[$k]['usre_grade']=$usre_grade['all_count'];
        }
        //unset($usre_grade['all_count']);
        
        foreach ($usre_push as $k => $v) {
           $count_percent = sprintf("%.0f",$v['count']/$usre_push['all_count']*100);
           $usre_push[$k]['count_percent']=$count_percent;
           $usre_push[$k]['usre_grade']=$usre_push['all_count'];
        }
        //unset($usre_push['all_count']);
        foreach ($all_grade as $k => $v) {
           $count_percent = sprintf("%.0f",$v['count']/$all_grade['all_count']*100);
           $all_grade[$k]['count_percent']=$count_percent;
           $all_grade[$k]['usre_grade']=$all_grade['all_count'];
        }
       // unset($usre_push['all_count']);
        $data = array('usre_grade'=>$usre_grade[0],'usre_push'=>$usre_push[0],'all_grade'=>$all_grade[0]);
        return ($data);
    }
	
	
	
	

    //注册后师傅列表
    public function master() {
        if(IS_POST){
             $plat_id = I('post.plat_id');
             $p = I('post.p');
             $p = $p*5;
        }else{
            $plat_id = I('get.plat_id');
            $p=0;
        }
        $pagenum = 5;
        
        if($plat_id) {
            $map['jg_recommend_platform.platform_id'] = $plat_id;
            $map['jg_recommend_platform.status'] = 1;
        } else {
            $map['jg_recommend_platform.status'] = 1;
            $group = 'jg_recommend_platform.recommend_id';
        }
        // $recom = D('RecommendPlatform')->get_all($map);
        $field = "jg_recommend_platform.*,jg_recommend.user_id,jg_recommend.fans,jg_recommend.year,jg_recommend.platform_num,jg_recommend.pit_num";
        $recom = M('RecommendPlatform')->join('RIGHT JOIN jg_recommend ON jg_recommend_platform.recommend_id = jg_recommend.id')->field($field)->order('jg_recommend.fans desc')->where($map)->group($group)->limit($p,$pagenum)->select();
        //echo M('RecommendPlatform')->_sql();die;
        $map = array();
        $recommend_id=0;
        foreach($recom as $k => $v) {
            if($recommend_id == $v['recommend_id']){
                unset($recom[$k]);
            }
            $recommend_id = $v['recommend_id'];
            $map = array();
            $map['id'] = $v['user_id'];
            $user = D('User')->get_one($map);
            $recom[$k]['user'] = $user;
             //派系
                $map = array();
                $map['user_id'] = $v['user_id'];
                $factions = M('Factions')->where($map)->find();
                $recom[$k]['factions'] = $factions;
            //技能
                $skill = M('Skill')->where($map)->find();
                $skill = explode(',',$skill['skill']);
                $recom[$k]['skill'] = $skill;
            //师傅圈子
                $map = array();
                $map['recommend_id'] = $v['id'];
                $platform = D('RecommendPlatform')->get_all($map,'*',0,2);
                $map = array();
                foreach($platform as $ke => $va) {
                    $map['id'] = $va['platform_id'];
                    $plat = D('Platform')->get_one($map,'name');
                    $plat_list[] = $plat['name'];
                    $recom[$k]['plat'] = $plat_list;
                }   
                 
                     
                    
                    

         }
        //print_r($recom);
        $nickname = $_SESSION['user']['nickname'];
        $this->assign('nickname',$nickname);
        $this->assign('recom',$recom);
        if(IS_POST){
            if(!$recom){
                $content =0;
            }else{
                $content = $this->fetch('master_item');
            }
            $this->ajaxReturn(array('content'=>$content));
        }else{
            $this->display();
        }
        
    }
	
	

}